package featuresWindows;

import helpFeaturesWindows.HelpBezierWindow;
import helpFeaturesWindows.HelpPocketWindow;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.vecmath.Point3d;

import operationSolids.OperationBezier;

import bReps.CreateBezierLinearPath;

import CAD.MainFrame;

import features.Bezier;
import features.Block;
import features.Feature;
import features.RoundFloorPocket;
import j3d.J3D;
import visualCAD.RoundFloorPocketWindowView;
import visualCAD.bezierWindowView;

public class CreateBezier extends bezierWindowView implements ActionListener
{
	private J3D j3d = null;
	private PocketDinamicTopPanel bezierTopPanel;
	private PocketDinamicBottomPanel bezierBottomPanel;
	float  positionX = 0,
			positionY = 0,	
			positionZ = 0,
			width=0;
	Point3d [] arrayP = new Point3d[6];
		
	public CreateBezier(J3D j3d, Block block)
	{
		super();
		
		this.j3d = j3d;
		
		/*bezierSplitPanel.setTopComponent(bezierTopPanel=new PocketDinamicTopPanel(
				positionX,positionY, 
				radius,r1,r2,r3,r4,
				width,length));
		bezierSplitPanel.setBottomComponent(bezierBottomPanel=new PocketDinamicBottomPanel(
				positionX,positionZ,
				radius,r1,r2,r3,r4,
				depth,width,length));*/
		bezierSplitPanel.setDividerLocation(0.5);
		
	//	double y = bezierTopPanel.faceY;
	//	double x = bezierTopPanel.faceX;
	//	double z = bezierTopPanel.faceZ;
		redrawDynamicComponents();
		setVisible(true);
		pack();
		this.init();
		
	}
	private void redrawDynamicComponents(){
		positionX=(Float)xPositionSpinner.getValue();
		positionY=(Float)yPositionSpinner.getValue();
		positionZ=(Float)zPositionSpinner.getValue();
		try{
			width=(Float)widthSpinner.getValue();
			arrayP[0] = new Point3d((Double) p1xSpinner.getValue(),
					(Double) p1ySpinner.getValue(),
					width);
			arrayP[1] = new Point3d((Double) p2xSpinner.getValue(),
					(Double) p2ySpinner.getValue(),
					width);
			arrayP[2] = new Point3d((Double) p3xSpinner.getValue(),
					(Double) p3ySpinner.getValue(),
					width);
			arrayP[3] = new Point3d((Double) p4xSpinner.getValue(),
					(Double) p4ySpinner.getValue(),
					width);
			arrayP[4] = new Point3d((Double) p5xSpinner.getValue(),
					(Double) p5ySpinner.getValue(),
					width);
			arrayP[5] = new Point3d((Double) p6xSpinner.getValue(),
					(Double) p6ySpinner.getValue(),
					width);
			
		}catch (Exception e) {
			e.printStackTrace();
			positionX=(Float)xPositionSpinner.getValue();
			positionY=(Float)yPositionSpinner.getValue();
			positionZ=(Float)zPositionSpinner.getValue();
				width=(Float)widthSpinner.getValue();
				arrayP[0] = new Point3d((Double) p1xSpinner.getValue(),
						(Double) p1ySpinner.getValue(),
						width);
				arrayP[1] = new Point3d((Double) p2xSpinner.getValue(),
						(Double) p2ySpinner.getValue(),
						width);
				arrayP[2] = new Point3d((Double) p3xSpinner.getValue(),
						(Double) p3ySpinner.getValue(),
						width);
				arrayP[3] = new Point3d((Double) p4xSpinner.getValue(),
						(Double) p4ySpinner.getValue(),
						width);
				arrayP[4] = new Point3d((Double) p5xSpinner.getValue(),
						(Double) p5ySpinner.getValue(),
						width);
				arrayP[5] = new Point3d((Double) p6xSpinner.getValue(),
						(Double) p6ySpinner.getValue(),
						width);
		}
	
			//setModels();
			/*bezierSplitPanel.setTopComponent(bezierTopPanel=new PocketDinamicTopPanel(
					positionX,positionY,
					radius,r1,r2,r3,r4,
					width,length));
			bezierSplitPanel.setBottomComponent(bezierBottomPanel=new PocketDinamicBottomPanel(
					positionX,positionZ,
					radius,r1,r2,r3,r4,
					depth,width,length));*/
			bezierSplitPanel.setDividerLocation(0.5);
	}
/*	void setModels(){
		
		SpinnerNumberModel lengthM = new SpinnerNumberModel(
				 ((Float)length).doubleValue(),((Float)(maiorRaio()*2)).doubleValue(),null,0.1);
		SpinnerNumberModel widthM = new SpinnerNumberModel(
				 ((Float)width).doubleValue(),((Float)(maiorRaio()*2)).doubleValue(),null,0.1);
		
		lengthSpinner.setModel(lengthM);
		widthSpinner.setModel(widthM);
		
		if(length<width){
			SpinnerNumberModel r1M = new SpinnerNumberModel(
					 ((Float)r1).doubleValue(),0,length/2,0.1);
			SpinnerNumberModel r2M = new SpinnerNumberModel(
					((Float)r2).doubleValue(),0,length/2,0.1);
			SpinnerNumberModel r3M = new SpinnerNumberModel(
					((Float)r3).doubleValue(),0,length/2,0.1);
			SpinnerNumberModel r4M = new SpinnerNumberModel(
					((Float)r4).doubleValue(),0,length/2,0.1);
			SpinnerNumberModel radiusM = new SpinnerNumberModel(
					((Float)radius).doubleValue(),0,length/2,0.1);
			
			r1Spinner.setModel(r1M);
			r2Spinner.setModel(r2M);
			r3Spinner.setModel(r3M);
			r4Spinner.setModel(r4M);
			radiusSpinner.setModel(radiusM);
			
		}else{
			SpinnerNumberModel r1M = new SpinnerNumberModel(
					 ((Float)r1).doubleValue(),0,width/2,0.1);
			SpinnerNumberModel r2M = new SpinnerNumberModel(
					((Float)r2).doubleValue(),0,width/2,0.1);
			SpinnerNumberModel r3M = new SpinnerNumberModel(
					((Float)r3).doubleValue(),0,width/2,0.1);
			SpinnerNumberModel r4M = new SpinnerNumberModel(
					((Float)r4).doubleValue(),0,width/2,0.1);
			SpinnerNumberModel radiusM = new SpinnerNumberModel(
					((Float)radius).doubleValue(),0,width/2,0.1);
			
			r1Spinner.setModel(r1M);
			r2Spinner.setModel(r2M);
			r3Spinner.setModel(r3M);
			r4Spinner.setModel(r4M);
			radiusSpinner.setModel(radiusM);
		}
		
	}*/
	
	public void init()
	{
		this.submitButton.addActionListener(this);
		helpButton.addActionListener(this);
		this.cancelButton.addActionListener(this);
		yPositionSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		zPositionSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		xPositionSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		widthSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		p1xSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		p1ySpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		p2xSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		p2ySpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		p3xSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		p3ySpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		p4xSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		p4ySpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		p5xSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		p5ySpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		p6xSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
		p6ySpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				redrawDynamicComponents();
			}
		});
	}
	public void actionPerformed(ActionEvent event) 
	{
		Object origin = event.getSource();
		if (origin.equals(cancelButton))
		{
			this.cancel();
		} else if (origin.equals(submitButton))
		{
			this.ok();
		}
		else if (origin.equals(helpButton)) 
		{
			new HelpBezierWindow();
		}
	}
	private void ok()
	{
		new Bezier(Feature.BEZIERPATH,null,nameTextField.getText(),positionX,positionY,positionZ,arrayP,MainFrame.Face);
		this.j3d.removeSolid();
		this.j3d.addSolid(MainFrame.block.block);
		this.dispose();
	}
	private void cancel()
	{
		dispose();
	}
}
